package com.qf.Servlet;

import com.qf.Service.UserService;
import com.qf.Service.impl.UserServiceImpl;
import com.qf.Utils.JsonUtils;
import com.qf.Utils.PageUtils;
import com.qf.entity.SysUser;
import com.qf.entity.UserManagEmentPaginationDisplay;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

@WebServlet(name = "userServlet", urlPatterns = "/userServlet")
public class UserServlet extends BaseServlet {
    private final UserService userService = new UserServiceImpl();

//    /**
//     * 验证验证码
//     * @param req
//     * @param response
//     */
//    public  void  checkedCode(HttpServletRequest req, HttpServletResponse response) throws IOException {
//        PrintWriter pw = response.getWriter();
//        HttpSession session = req.getSession();
//        //调用userService里生成4为验证码的checkCode方法
//        String code = userService.checkCode();
//        //创建一个返回给前端的json实体类对象
//        ResultVo<String> resultVo = new ResultVo<>();
//
//        //检查验证码获取成功与否
//        if (code==null || "".equals(code)){
//            resultVo.setCode(500);
//            resultVo.setMessage("失败");
//            resultVo.setData(code);
//        }else{
//            //将请求头设置为200
//            resultVo.setCode(200);
//            //请求提示设置为成功
//            resultVo.setMessage("成功");
//            //请求内容设置为被混淆的验证码
//            resultVo.setData(code);
//            //将验证码存入到session
//            session.setAttribute("code",code);
//        }
//        JsonUtils.showJson(pw,resultVo);
//    }

    public void look_All(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        System.out.println("执行look_All方法");
        //接收前端传递的参数
        String currentPageNo = req.getParameter("currentPageNo");
        String pageSize = req.getParameter("pageSize");
        //调用service层方法
        PageUtils<SysUser> pageUtils = userService.selectByPage(currentPageNo, pageSize);
        JsonUtils.jsonTransfer(resp.getWriter(), req.getSession(), pageUtils);
    }

    public void add(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //接收前端传递的参数
//        String nid = req.getParameter("nid");
//        String password = req.getParameter("password");
        SysUser u = sysUserAddUtils(req, resp);
//        //调用
        int num = userService.addUser(u);
        //TODO 工具类使用示例
        JsonUtils.jsonTransfer(resp.getWriter(), req.getSession(), num);
    }

    public void idByUser(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        int id = Integer.parseInt(req.getParameter("id"));
        SysUser sysUser = userService.idByUser(id);
        JsonUtils.jsonTransfer(resp.getWriter(), req.getSession(), sysUser);
    }

    public void modify(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        int id = Integer.parseInt(req.getParameter("id"));
        SysUser u = sysUserAddUtils(req, resp);
        int num = userService.modify(u);
        JsonUtils.jsonTransfer(resp.getWriter(), req.getSession(), num);
    }

    /**
     * 删除
     *
     * @param req
     * @param resp
     */
    public void delUser(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        int id = Integer.parseInt(req.getParameter("id"));
        int num = userService.delUser(id);
        JsonUtils.jsonTransfer(resp.getWriter(), req.getSession(), num);
    }

    public void resetPassword(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        int id = Integer.parseInt(req.getParameter("id"));
        String resetpassword = userService.resetPassword(id);
        JsonUtils.jsonTransfer(resp.getWriter(), req.getSession(), resetpassword);
    }

    /**
     * 重置密码
     */
    public void freeze(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        int id = Integer.parseInt(req.getParameter("id"));
        int freeze = userService.freeze(id);
        JsonUtils.jsonTransfer(resp.getWriter(), req.getSession(), freeze);
    }

    public void unfreeze(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        int id = Integer.parseInt(req.getParameter("id"));
        int freeze = userService.unfreeze(id);
        JsonUtils.jsonTransfer(resp.getWriter(), req.getSession(), freeze);
    }

    public void roleAssignment(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        int id = Integer.parseInt(req.getParameter("id"));
        String roleid = req.getParameter("roleid");
        int freeze = userService.roleAssignment(id, roleid);
        JsonUtils.jsonTransfer(resp.getWriter(), req.getSession(), freeze);
    }

    public void ConditionalQuery(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        String str = req.getParameter("str");
        String startTime = req.getParameter("startTime");
        String endTime = req.getParameter("endTime");
        Integer currentPageNo = Integer.valueOf(req.getParameter("currentPageNo"));
        Integer pageSize = Integer.valueOf(req.getParameter("pageSize"));
        //调用service层方法
        List<UserManagEmentPaginationDisplay> list = userService.ConditionalQuery(str, startTime, endTime, currentPageNo, pageSize);
        JsonUtils.jsonTransfer(resp.getWriter(), req.getSession(), list);
    }

    /**
     * 工具类
     *
     * @param req
     * @param resp
     * @return
     */
    public SysUser sysUserAddUtils(HttpServletRequest req, HttpServletResponse resp) {
        SysUser u = new SysUser();
        u.setAccount(req.getParameter("account"));
        u.setPassword(req.getParameter("password"));
        u.setName(req.getParameter("name"));
        u.setSex(Long.parseLong(req.getParameter("sex")));
        u.setDeptid(Long.parseLong(req.getParameter("deptid")));
        u.setEmail(req.getParameter("email"));
        u.setPhone(req.getParameter("phone"));
        u.setStatus(Long.parseLong(req.getParameter("status")));
        return u;
    }

}
